home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr53 / pctv4n_1.zip / WLIST.CPP < prev    next >
C/C++ Source or Header  |  1993-06-10  |  4KB  |  125 lines

  1. // Text file lister -- MDI Windows application by Tom Swan
  2.  
  3. #include <commdlg.h>
  4. #include <owl.h>
  5. #include <mdi.h>
  6. #include "wlist.rch"
  7. #include "status.h"
  8. #include "child.h"
  9.  
  10. // The MDI application class
  11. class TWListApp: public TApplication {
  12. public:
  13.   TWListApp(LPSTR AName, HINSTANCE AnInstance,
  14.     HINSTANCE APrevInstance, LPSTR ACmdLine, int ACmdShow)
  15.     : TApplication(AName, AnInstance, APrevInstance,
  16.       ACmdLine, ACmdShow) {};
  17.   virtual void InitMainWindow();
  18. };
  19.  
  20. // The MDI main window class (the frame)
  21. class TMainWindow: public TMDIFrame {
  22. public:
  23.   TMainWindow(LPSTR ATitle, int MenuId);
  24.   void ResizeClientRect();
  25.   virtual void CMFileOpen(RTMessage)
  26.     = [CM_FIRST + CM_MDIFILEOPEN];
  27.   virtual void WMSize(RTMessage)
  28.     = [WM_FIRST + WM_SIZE];
  29.   virtual void WMMenuSelect(RTMessage)
  30.     = [WM_FIRST + WM_MENUSELECT];
  31. private:
  32.   PTStatusBar pStatusBar;   // Pointer to status bar object
  33.   char filterStr[BUFSIZE];  // File dialog filter list
  34.   int filterIndex;          // File dialog filter number
  35. };
  36.  
  37. // Initialize the MDI application's main (frame) window
  38. void TWListApp::InitMainWindow()
  39. {
  40.   MainWindow = new TMainWindow("Text File Viewer", ID_MENU);
  41. }
  42.  
  43. // Construct main window object
  44. TMainWindow::TMainWindow(LPSTR ATitle, int MenuId)
  45.   : TMDIFrame(ATitle, MenuId)
  46. {
  47.   filterIndex = 1;
  48.   if (LoadString(GetApplication()->hInstance, STR_FILEFILTERS,
  49.       filterStr, sizeof(filterStr)) == 0)
  50.     filterStr[0] = 0;
  51.   else {
  52.     int fslen = strlen(filterStr);
  53.     for (int i = 0; i < fslen; i++)
  54.       if (filterStr[i] == '|')
  55.         filterStr[i] = 0;
  56.   }
  57.   ChildMenuPos = 2;
  58.   pStatusBar = new TStatusBar(this);
  59. }
  60.  
  61. // Send UM_CalcParentClientRect message to child windows
  62. void ResizeChildWindows(Pvoid P, Pvoid Param)
  63. {
  64.   HWND hWnd = ((PTWindow)P)->HWindow;
  65.   if (hWnd) SendMessage(hWnd, 
  66.     UM_CalcParentClientRect, FALSE, (LPARAM)Param);
  67. }
  68.  
  69. // Resize client window rectangle
  70. void TMainWindow::ResizeClientRect()
  71. {
  72.   RECT R;  // Client rectangle
  73.   GetClientRect(HWindow, &R);
  74.   ForEach(ResizeChildWindows, &R);
  75.     SetWindowPos(ClientWnd->HWindow, 0, R.left, R.top,
  76.       R.right - R.left, R.bottom - R.top, SWP_NOZORDER);
  77. }
  78.  
  79. // Open a file by displaying a common file dialog
  80. void TMainWindow::CMFileOpen(RTMessage)
  81. {
  82.   OPENFILENAME fn;         // Common dialog structure
  83.   char tempName[BUFSIZE];  // Holds filename
  84.   memset(&fn, 0, sizeof(fn));
  85.   strcpy(tempName, "");
  86.   fn.lStructSize = sizeof(OPENFILENAME);
  87.   fn.hwndOwner = HWindow;
  88.   fn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  89.   fn.lpstrFile = tempName;
  90.   fn.nMaxFile = sizeof(tempName);
  91.   fn.lpstrFilter = filterStr;
  92.   fn.nFilterIndex = filterIndex;
  93.   if (GetOpenFileName(&fn)) {
  94.     filterIndex = fn.nFilterIndex;
  95.     GetApplication()->MakeWindow(new
  96.       TChildWindow(this, fn.lpstrFile));
  97.   }
  98. }
  99.  
  100. // Main window size is changing
  101. void TMainWindow::WMSize(RTMessage Msg)
  102. {
  103.   TWindow::WMSize(Msg);
  104.   if (pStatusBar) 
  105.     pStatusBar->AdjustSize(Msg.LP.Lo, Msg.LP.Hi);
  106.   ResizeClientRect();  // Update client rectangle
  107. }
  108.  
  109. // Notate menu selections by passing message to child window
  110. void TMainWindow::WMMenuSelect(RTMessage Msg)
  111. {
  112.   if (pStatusBar)
  113.     SendMessage(pStatusBar->HWindow, WM_MENUSELECT,
  114.       Msg.WParam, Msg.LParam);  
  115. }
  116.  
  117. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  118.   LPSTR lpszCmdLine, int nCmdShow)
  119. {
  120.   TWListApp WListApp("WListApp", hInstance, hPrevInstance,
  121.     lpszCmdLine, nCmdShow);
  122.   WListApp.Run();
  123.   return WListApp.Status;
  124. }
  125.